// Democratization as an Impetus for Peace Talks in Civil Wars by Barış Arı
// Replication files 
// 9 January 2022

import delimited "data.csv", clear 
tsset dyadid year

// main explanatory variable: demref
tab negotiation demref

// Defining main explanatory variable(s)
global mes1 polyarchy demref

// time dimension (cubic polynomials)
global poly  tsln tsln2 tsln3


//////////// TABLE 1 : MODELS 1-4 ///////////////////
global cont1 duration_dy_ln impgdppcln imppopln i.intensity i.region
global cont2 duration_dy_ln impgdppcln imppopln i.intensity i.region i.rebstrbinimp i.ethnicincomp i.exchange odapcln i.medprev2y
global cont3 duration_dy_ln impgdppcln imppopln i.intensity i.region i.rebstrbinimp i.ethnicincomp i.exchange odapcln i.medprev2y bdbest_ln unpkototalln

// Model 1
logit negotiation $mes1 $cont1 $poly, robust cluster(conflictid)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m1
outreg2 m1 using table1.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') replace 

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x

// number of dyads in sample
egen x = group(dyadid) if e(sample) == 1
sum x
drop x

// Model 2
logit negotiation $mes1 $poly $cont2, robust cluster(conflictid)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m2
outreg2 m2 using table1.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') append

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x

// number of dyads in sample
egen x = group(dyadid) if e(sample) == 1
sum x
drop x

// Model 3
logit negotiation $mes1 $cont3 $poly , robust cluster(conflictid)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m3
outreg2 m3 using table1.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') append

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x

// number of dyads in sample
egen x = group(dyadid) if e(sample) == 1
sum x
drop x


// Model 4 conditional logit (fixed-effect)
global cont4 duration_dy_ln impgdppcln imppopln i.intensity i.exchange odapcln i.medprev2y bdbest_ln unpkototalln
xtset dyadid year

// clogit negotiation $mes1  $poly $cont4, group(dyadid) / this should be equivalent below with xtset and xtlogit
xtlogit negotiation $mes1  $poly $cont4, fe 
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m4
outreg2 m4 using table1.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') append

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x

// number of dyads in sample
egen x = group(dyadid) if e(sample) == 1
sum x
drop x

////////////// TABLE 1: MODELS 5-6 STATE LEVEL ANALYSIS ////////////////
logit negotiation $mes1 $cont3 $poly , robust cluster(conflictid)
keep if e(sample) == 1

keep gwnoa year negotiation polyarchy demref duration_dy_ln impgdppcln imppopln intensity region rebstrbinimp ethnicincomp exchange odapcln medprev2y bdbest_ln unpkototalln

collapse (max) negotiation polyarchy demref duration_dy_ln impgdppcln imppopln intensity region rebstrbinimp ethnicincomp exchange odapcln medprev2y unpkototalln (sum) bdbest_ln, by(gwnoa year)
tsset gwnoa year

// we need btsc package here. see: http://www-personal.umich.edu/~wmacmill/stata/btscs/btscs-a-binary-time.pdf
btscs negotiation year gwnoa, g(tsln)
gen tsln2 = tsln^2
gen tsln3 = tsln^3

logit negotiation $mes1 $cont3 tsln tsln2 tsln3, robust cluster(gwnoa)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m5

outreg2 m5 using table1.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') append

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x

// Model 6 (state-level fixed effects)
xtlogit negotiation $mes1 $cont4 tsln tsln2 tsln3, fe
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m6
outreg2 m6 using table1.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') append


////////////////////////////// TABLE 2 MODELS 7-8 //////////////////////////////////
import delimited "data.csv", clear 

// model control variables
global cont_MT duration_dy_ln impgdppcln imppopln i.intensity i.rebstrbinimp i.ethnicincomp exchange medprev2y odapcln i.region

// Defining main explanatory variable(s)
global mes1 polyarchy demref

// First Order Markov Transition Models
sort dyadid year
gen first = 0
by dyadid: replace first = 1 if _n == 1

tsset dyadid year

// TABLE 2 : Model 7
logit negotiation $mes1  $cont_MT  if l.negotiation == 0 & first != 1 , robust cluster(conflictid)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m7
outreg2 m7 using table2.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') replace

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x

// number of dyads in sample
egen x = group(dyadid) if e(sample) == 1
sum x
drop x

// Model 8 : MT continuation
logit negotiation $mes1 $cont_MT if l.negotiation == 1 & first != 1, robust cluster(conflictid)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m8
outreg2 m8 using table2.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') append

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x

// number of dyads in sample
egen x = group(dyadid) if e(sample) == 1
sum x
drop x

//////// Model 9: Dyad-level inaugural negotiation //////////////
import delimited "data.csv", clear 

tsset dyadid year

// Defining main explanatory variable(s)
global poly tsln tsln2 tsln3
global cont_inaug duration_dy_ln impgdppcln imppopln i.intensity i.rebstrbinimp i.ethnicincomp exchange i.region odapcln  medprev2y


logit  negotiation_inaugural polyarchy demref  $cont_inaug if negotiation_inaugural >=0, robust cluster(dyadid)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m9
outreg2 m9 using table3.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') replace

// number of countries in sample
egen x = group(gwnoa) if e(sample) == 1
sum x
drop x


// number of dyads in sample
egen x = group(dyadid) if e(sample) == 1
sum x
drop x

////////////////// Model 10: State level
import delimited "data.csv", clear 

global poly tsln tsln2 tsln3
global cont_inaug2 duration_dy_ln impgdppcln imppopln i.intensity i.rebstrbinimp i.ethnicincomp exchange i.region odapcln

logit negotiation demref polyarchy $cont_inaug2 , robust cluster(gwnoa)
keep if e(sample) == 1

keep gwnoa year negotiation_inaugural polyarchy demref duration_dy_ln impgdppcln imppopln intensity region rebstrbinimp ethnicincomp exchange odapcln bdbest_ln unpkototalln

collapse (min) negotiation_inaugural (max) polyarchy demref duration_dy_ln impgdppcln imppopln intensity region rebstrbinimp ethnicincomp exchange odapcln unpkototalln (sum) bdbest_ln, by(gwnoa year)

tsset gwnoa year

logit  negotiation_inaugural polyarchy  demref $cont_inaug2 if negotiation_inaugural >=0, robust cluster(gwnoa)
estat ic
mat es_ic = r(S)
local AIC: display %4.1f es_ic[1,5]
est store m10
outreg2 m10 using table3.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(AIC, `AIC') append
